package com.wanmait.wisdom.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wisdom.entity.Unit;
import com.wanmait.wisdom.manage.service.ManageUnitService;
import com.wanmait.wisdom.mapper.UnitMapper;
import com.wanmait.wisdom.util.AjaxResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class ManageUnitServiceImpl extends ServiceImpl<UnitMapper, Unit> implements ManageUnitService {
    @Resource
    private UnitMapper unitMapper;

    @Override
    public AjaxResult checkunit(Unit unit) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("unit_name", unit.getUnitName());
        queryWrapper.eq("building_id", unit.getBuildingId());
        Unit unit1 = unitMapper.selectOne(queryWrapper);
        if(unit1!=null){
            return AjaxResult.success("已存在");
        }
        else{
            save(unit);
            return AjaxResult.success("添加成功");
        }
    }

    @Override
    public AjaxResult findById(Integer id) {
        return AjaxResult.success().put("unit",unitMapper.selectById(id));
    }

    @Override
    public AjaxResult findUnitNumberOptions(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("building_id",id);
         List<Unit> units = unitMapper.selectList(queryWrapper);
        return AjaxResult.success().put("findUnitNumberOptions", units);
    }

    @Override
    public AjaxResult findUnitBybuildingid(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("building_id",id);
        List<Unit> units = unitMapper.selectList(queryWrapper);
        return AjaxResult.success("查询成功").put("units",units);
    }
    @Override
    public List<Unit> selectUnitById(Integer id) {
        List<Unit> unitList =  unitMapper.selectUnitByBuildingId(id);
        return  unitList;
    }
}
